package com.wondersgroup.fjzlpt.org.dao.impl.zy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.org.dao.inter.zy.ZyDAO;

@Repository
public class ZyDAOImpl extends BaseJTDAOImpl implements ZyDAO{

	@Override
	@Autowired
	@Qualifier("TB_ZD_YLJGXX")
	public void setTb(TableBean tb) {
		super.setTb(tb);
	}
	/**
	 * 查询表数据
	 * @param map
	 * @return JSONArray
	 */
	public JSONArray queryTable(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select * from " + map.get("TABLENAME") + " where 1=1 ");
		List list = new ArrayList();
		if(!map.containsKey("XMMC")){
			sbud.append(" and JLZT = '1' ");
		}
		String value = map.get("XMMC");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append(" and XMMC like ? ");
			list.add("%"+value+"%");
		}
		SQLBean sb = new SQLBean();
		sb.setSql(sbud.toString());
		sb.setParamAry(list);
		return super.queryList(sb);
	}
	
	@Override
	public JSONObject queryPageTable(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("select * from " + map.get("TABLENAME") + " where 1=1 ");
		List list = new ArrayList();
		String value = map.get("XMMC");
		if(!StringUtils.isEmpty(value))
		{
			sbud.append(" and XMMC like ? ");
			list.add("%"+value+"%");
		}
		PageQuerySQLBean sb = super.checkAndCreatePageQuerySQLBean(map, sbud.toString(), "order by XMDM ", list.toArray());
		return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
	}
	
	@Override
	public void deleteOne(Map<String, String> map) {
		SQLBean sb = new SQLBean();
		StringBuilder sbud = new StringBuilder();
		if("1".equals(map.get("SFDJZY"))){
			sbud.append("delete from " + map.get("TABLENAME") + " where XMDM = ? ");
			sb = new SQLBean(sbud.toString(), new String[]{map.get("XMDM")});
		}else{
			sbud.append("delete from " + map.get("TABLENAME") + " where XMDM = ? and SJDM = ? ");
			sb = new SQLBean(sbud.toString(), new String[]{map.get("XMDM"), map.get("XMDM")});
		}
		
		
		super.update(sb);
	}
	
	@Override
	public void insertOne(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("insert into " + map.get("TABLENAME")  +" (XMDM, XMMC,");
		String value = "";
		if("1".equals(map.get("SFDJZY"))){
			value = map.get("XMSM");
			sbud.append("XMSM,");
		}else{
			value = map.get("SJDM");
			sbud.append("SJDM,");
		}
		sbud.append("JLZT) values (?,?,?,?)");
		SQLBean sb = new SQLBean(sbud.toString(), new String[]{map.get("XMDM"),map.get("XMMC"),value, map.get("JLZT")});
		super.update(sb);
		
	}
	
	@Override
	public void updateOne(Map<String, String> map) {
		StringBuilder sbud = new StringBuilder("update " + map.get("TABLENAME") + " set XMMC = ?,JLZT = ?, ");
		String value = "";
		if("1".equals(map.get("SFDJZY"))){
			value = map.get("XMSM");
			sbud.append("XMSM = ? ");
		}else{
			value = map.get("SJDM");
			sbud.append("SJDM = ? ");
		}
		sbud.append(" where XMDM = ? ");
		SQLBean sb = new SQLBean(sbud.toString(), new String[]{map.get("XMMC"), map.get("JLZT"), value, map.get("XMDM")});
		super.update(sb);
	}
	

}
